package net.lab1024.sa.admin.module.business.file.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.file.dao.*;
import net.lab1024.sa.admin.module.business.file.domain.entity.*;
import net.lab1024.sa.admin.module.business.file.domain.form.add.DocmentAddForm;
import net.lab1024.sa.admin.module.business.file.domain.form.query.DocmentQueryForm;
import net.lab1024.sa.admin.module.business.file.domain.form.update.DocmentUpdateForm;
import net.lab1024.sa.admin.module.business.file.domain.vo.*;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import net.lab1024.sa.base.common.util.SmartRequestUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 课程分类 Service
 *
 * @Author AKITA
 * @Date 2025-07-04 16:55:11
 * @Copyright AKITA
 */

@Service
public class DocmentService {

    @Resource
    private DocmentDao docmentDao;
    @Resource
    private DocmentaDao docmentaDao;
    @Resource
    private DocmentbDao docmentbDao;
    @Resource
    private DocmentaaDao docmentaaDao;
    @Resource
    private DocmentabDao docmentabDao;


    @Resource
    private FiletypeService filetypeService;


    public PageResult<DocmentVO> queryPage(DocmentQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        queryForm.setUserId(SmartRequestUtil.getRequestUserId());
        List<Long> longs = null;
        if (queryForm.getTypeId() != null) {
            longs = filetypeService.selectTreeIds(queryForm.getTypeId());
        }
        List<DocmentVO> list = docmentDao.queryPage(page, queryForm, longs);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    public PageResult<DocmentVO> queryMyPage(DocmentQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        queryForm.setCreateUserId(SmartRequestUtil.getRequestUserId());
        List<Long> longs = null;
        if (queryForm.getTypeId() != null) {
            longs = filetypeService.selectTreeIds(queryForm.getTypeId());
        }
        List<DocmentVO> list = docmentDao.queryPage(page, queryForm, longs);
        return SmartPageUtil.convert2PageResult(page, list);
    }


    public ResponseDTO<Long> add(DocmentAddForm addForm) {

        /*
         * 校验系统是否存在同样名称的文件
         *
         *
         * */
        List<DocmentEntity> docmentEntities = docmentDao.selectList(new LambdaQueryWrapper<DocmentEntity>().like(DocmentEntity::getName, addForm.getName()));
        if (docmentEntities != null && !docmentEntities.isEmpty()) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST, "相同名称的文件只允许上传一份");
        }


        List<DocmentEntity> docmentEntitiesa = docmentDao.selectList(new LambdaQueryWrapper<DocmentEntity>().eq(DocmentEntity::getTypeId, addForm.getTypeId()));
        if (docmentEntitiesa != null && !docmentEntitiesa.isEmpty()) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST, "相同文件类型的文件只允许上传一份");
        }

        DocmentEntity docmentEntity = SmartBeanUtil.copy(addForm, DocmentEntity.class);
        docmentDao.insert(docmentEntity);
        return ResponseDTO.ok(docmentEntity.getDocEntry());
    }

    @Transactional
    public ResponseDTO<String> update(DocmentUpdateForm updateForm) {
        DocmentEntity docmentEntity = SmartBeanUtil.copy(updateForm, DocmentEntity.class);
        List<DocmentaEntity> docmentaEntities = SmartBeanUtil.copyList(updateForm.getLista(), DocmentaEntity.class);
        List<DocmentbEntity> docmentbEntities = SmartBeanUtil.copyList(updateForm.getListb(), DocmentbEntity.class);

        if (docmentDao.updateById(docmentEntity) == 0) {
            throw new RuntimeException("数据修改失败！刷新后重试");
        }
        return ResponseDTO.ok();
    }


    public ResponseDTO<String> batchDelete(List<Long> idList) {
        docmentDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    public ResponseDTO<String> delete(Long docEntry) {
        docmentDao.deleteById(docEntry);
        docmentaDao.deleteById(docEntry);
        docmentbDao.deleteById(docEntry);
        docmentaaDao.deleteById(docEntry);
        docmentabDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }

    public ResponseDTO<DocmentVO> info(Long docEntry) {
        DocmentEntity docmentEntity = docmentDao.selectById(docEntry);
        DocmentVO copy = SmartBeanUtil.copy(docmentEntity, DocmentVO.class);
        List<DocmentaEntity> docmentaEntities = docmentaDao.selectList(new LambdaQueryWrapper<DocmentaEntity>().eq(DocmentaEntity::getDocEntry, docEntry).orderByDesc(DocmentaEntity::getLineNum));
        List<DocmentbEntity> docmentbEntities = docmentbDao.selectList(new LambdaQueryWrapper<DocmentbEntity>().eq(DocmentbEntity::getDocEntry, docEntry));
        List<DocmentaaEntity> docmentaaEntities = docmentaaDao.selectList(new LambdaQueryWrapper<DocmentaaEntity>().eq(DocmentaaEntity::getDocEntry, docEntry).orderByDesc(DocmentaaEntity::getLineNum));
        List<DocmentabEntity> docmentabEntities = docmentabDao.selectList(new LambdaQueryWrapper<DocmentabEntity>().eq(DocmentabEntity::getDocEntry, docEntry).orderByDesc(DocmentabEntity::getLineNum));
        List<DocmentaVO> docmentaVOS = SmartBeanUtil.copyList(docmentaEntities, DocmentaVO.class);
        List<DocmentbVO> docmentbVOS = SmartBeanUtil.copyList(docmentbEntities, DocmentbVO.class);
        List<DocmentaaVO> docmentaaVOS = SmartBeanUtil.copyList(docmentaaEntities, DocmentaaVO.class);
        List<DocmentabVO> docmentabVOS = SmartBeanUtil.copyList(docmentabEntities, DocmentabVO.class);
        copy.setLista(docmentaVOS);
        copy.setListb(docmentbVOS);
        copy.setListaa(docmentaaVOS);
        copy.setListab(docmentabVOS);
        return ResponseDTO.ok(copy);
    }

    Boolean updateVersion(Long docEntry, Integer version) {
        DocmentEntity docmentEntity = new DocmentEntity();
        docmentEntity.setDocEntry(docEntry);
        docmentEntity.setVersion(version);
        if (docmentDao.updateById(docmentEntity) == 0) {
            throw new RuntimeException("数据修改失败！刷新后重试");
        }
        return true;
    }


    Boolean chackVersion(Long docEntry, Integer version) {
        DocmentEntity docmentEntity = docmentDao.selectOne(new LambdaQueryWrapper<DocmentEntity>().eq(DocmentEntity::getDocEntry, docEntry).eq(DocmentEntity::getVersion, version));
        if (docmentEntity == null) {
            throw new RuntimeException("数据修改失败！刷新后重试");
        }
        return true;
    }

}
